package com.atguigu.java;


import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * Title: JDKProxy
 * Description: TODO
 *
 * @author gejx
 * @version V1.0
 * @date 2021-12-07
 */
public class JDKProxy {
    public static void main(String[] args) {
        //通过反射获取类
        Class [] interfaces={UserDao.class};
        UserDaoImpl userDaoImpl=new UserDaoImpl();
        UserDao userDao= (UserDao) Proxy.newProxyInstance(JDKProxy.class.getClassLoader(), interfaces, new UserDaoProxy(userDaoImpl));
        String a = userDao.A("1", "2");
        System.out.println("a"+a);
    }
}
//动态代理操作
class  UserDaoProxy implements InvocationHandler{
    private  Object obj;
    //创建代理对象，把需要代理的对象传进来
    public  UserDaoProxy(Object obj){
        this.obj=obj;
    }
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("方法执行前执行");
        Object invoke = method.invoke(obj, args);
        System.out.println("方法执行后执行");
        return invoke;
    }
}
